import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

/**
 * RSA加解密
 * @author xiexiaojin
 * @date 2025-03-07 17:10
 */
public class RSADecrypt {
    public static String publicKey="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgF19VqIKD7+OFPcuLVC38KpnHynqc3TfY8YMcg6wuX8IF0CmKriRH3V6NoVNQdWJfyRlmYMTPnejYm1bSKGuWe3Pfx5RyhpkFF41OYo2UBxvBHfmt8C255knlKb4v73it6GMuUUgt1aM8b5X7Bkv+RZgnDU0IoOrVXZpZ+qE7XsD9ymMazGKljoeI1wgcI+fzXGP/vV0X5y/vsRGxalmDe4Vnwwshs+GqOuPJKQeOctN/vWUc0uYY2WBeCOLd7dgvWuUPcB2Au0FN7cBl/v/AHysY/6HkLv8fCPOz36IRFX8vd2Mn+OMVHdNit0mwoHUIS9IxQVkYl+BYUbu78MhxwIDAQAB";  //公钥
    public static String privateKey="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCAXX1WogoPv44U9y4tULfwqmcfKepzdN9jxgxyDrC5fwgXQKYquJEfdXo2hU1B1Yl/JGWZgxM+d6NibVtIoa5Z7c9/HlHKGmQUXjU5ijZQHG8Ed+a3wLbnmSeUpvi/veK3oYy5RSC3VozxvlfsGS/5FmCcNTQig6tVdmln6oTtewP3KYxrMYqWOh4jXCBwj5/NcY/+9XRfnL++xEbFqWYN7hWfDCyGz4ao648kpB45y03+9ZRzS5hjZYF4I4t3t2C9a5Q9wHYC7QU3twGX+/8AfKxj/oeQu/x8I87PfohEVfy93Yyf44xUd02K3SbCgdQhL0jFBWRiX4FhRu7vwyHHAgMBAAECggEAX1aQd96jwGySN2oaptdztlCphJDaK+Ra4jy/E5trkGoT9Ukz4TjxuD7FhWbeKY5Sr9a1ix26FkQ/WiREjx5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8NWv09gIpmm6C4IzsI8faEQVen3GI0S3lz3NheGncLPZ/n23H6Y7d1i/6Z+IpQg="; //私钥
    // 使用示例
    public static void main(String[] args) {

        String encrypted = "DsjkQLVJGTlJiJ7hSF8Cgv7FJlVPcdzGN83wKHJpqqIJWlW5xPMa8nQoTH0FKHupWYb/LF9MKc6PC04Z0L0wmCxKcgaLqF/s/QZWCSeSOudcHRAdEBBafV9T4Tp0D8vQBNwUmEqPdZx5qdjv/QfwfiNINvz73GjoDTeU3NpwrvuNy3IRtiZzaORLhPaM/QZGHbbZlLD88mzWv0tzxqohmLQSyNRo8BAhEspreHYyqaoQzYdfQoF57ZS9omDaLSovFdWSgE6xZHX0beo07AmisVlkGpfv74/g2bXEaLmDLHwPlSur8i0/8bxRwHwnJfS+wIwE2ad2o3kBL1fXw9y74g==";
        // 解密
        String decrypted = decrypt(encrypted, privateKey);
        System.out.println("解密后：" + decrypted);

        // 加密
//        String encrypted = encrypt(content, publicKey);
//        System.out.println("加密后：" + encrypted);


    }
    /**
     * RSA公钥加密
     * @param content 待加密内容
     * @param publicKey 公钥
     * @return 加密后的内容(Base64编码)
     */
    public static String encrypt(String content, String publicKey) {
        try {
            byte[] decoded = Base64.getDecoder().decode(publicKey);
            PublicKey pubKey = KeyFactory.getInstance("RSA")
                    .generatePublic(new X509EncodedKeySpec(decoded));
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, pubKey);
            byte[] result = cipher.doFinal(content.getBytes());
            return Base64.getEncoder().encodeToString(result);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * RSA私钥解密
     * @param content 待解密内容(Base64编码)
     * @param privateKey 私钥
     * @return 解密后的内容
     */
    public static String decrypt(String content, String privateKey) {
        try {
            byte[] inputByte = Base64.getDecoder().decode(content);
            byte[] decoded = Base64.getDecoder().decode(privateKey);
            PrivateKey priKey = KeyFactory.getInstance("RSA")
                    .generatePrivate(new PKCS8EncodedKeySpec(decoded));
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE, priKey);
            byte[] result = cipher.doFinal(inputByte);
            return new String(result);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }



}
